package com.data4.user.controller;


import com.data4.feignapi.client.OrderClient;
import com.data4.feignapi.client.SupplierClient;
import com.data4.feignapi.common.constants.GatewayConstants;
import com.data4.feignapi.dto.OrderDTO;
import com.data4.feignapi.dto.PhonePwdLoginDTO;
import com.data4.feignapi.dto.Result;
import com.data4.feignapi.dto.UserDTO;
import com.data4.user.service.DUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Forever
 * @since 2024-12-11
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户操作")
public class DUserController {
    @Resource
    private DUserService dUserService;
    @Resource
    private OrderClient orderClient;
    @Resource
    private SupplierClient supplierClient;

    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public Result register(@RequestBody UserDTO userDTO) {
        return dUserService.register(userDTO);
    }

    @GetMapping("/getUserInfoById")
    @Operation(summary = "根据用户id查询用户信息")
    public Result getUserInfoById(@RequestHeader(GatewayConstants.X_USER_ID) String userId) {
        return dUserService.getUserInfoById(Integer.parseInt(userId));
    }
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result login(@RequestBody PhonePwdLoginDTO pwdLoginDTO) {
        return dUserService.login(pwdLoginDTO);
    }

    @PostMapping("/addOrder")
    @Operation(summary = "添加订单")
    public Result addOrder(@RequestBody OrderDTO orderDTO,
                           @RequestHeader(GatewayConstants.X_USER_ID) String userId) {
        System.err.println("userId:" + userId);
        return orderClient.addOrder(orderDTO, Integer.parseInt(userId));
    }

    @Operation(summary = "分页查询订单，按时间降序")
    @GetMapping("/getOrderByTime/{current}/{num}")
    public Result getOrderByTime(@PathVariable Long current, @PathVariable Long num
            , @RequestHeader(GatewayConstants.X_USER_ID) String userId) {
        return orderClient.getOrderByTime(current, num, Integer.parseInt(userId));
    }

    @Operation(summary = "分组查询")
    @PostMapping("/getOrderGroup")
    public Result getOrderGroup(@RequestBody List<String> groupIds,
                                @RequestHeader(GatewayConstants.X_USER_ID) String userId) {
        return orderClient.getOrderGroup(groupIds, Integer.parseInt(userId));
    }

    @Operation(summary = "根据总价钱范围查询")
    @GetMapping("/getOrderByPriceScope/{current}/{num}")
    public Result getOrderByPriceScope(@PathVariable("current") Integer current,
                                       @PathVariable("num") Integer num,
                                       @RequestParam(required = false) Integer little,
                                       @RequestParam(required = false) Integer big,
                                       @RequestHeader(GatewayConstants.X_USER_ID) String userId) {
        return orderClient.getOrderByPriceScope(current, num,
                little, big, Integer.parseInt(userId));
    }

    @Operation(summary = "查询订单详细信息")
    @GetMapping("/getOrder/{orderId}")
    Result getOrder(@PathVariable Integer orderId,
                    @RequestHeader(GatewayConstants.X_USER_ID) String userId) {
        return orderClient.getOrder(orderId, Integer.parseInt(userId));
    }

    @Operation(summary = "根据产品名称模糊查订单")
    @GetMapping("/getOrderByProductName/{ProductCurrent}/{ProductPageNum}")
    Result getOrderByProductName(@PathVariable Integer ProductCurrent,
                                 @PathVariable Integer ProductPageNum,
                                 @RequestParam("productName") String productName,
                                 @RequestHeader(GatewayConstants.X_USER_ID) String userId) {
        return orderClient.getOrderByProductName(ProductCurrent, ProductPageNum,
                productName, Integer.parseInt(userId));
    }

    @GetMapping("/getProductDetail/{productId}")
    @Operation(summary = "根据商品id查询商品详情")
    public Result getProductDetail(@PathVariable Integer productId) {
        return Result.ok(supplierClient.getProductDetail(productId));
    }
}

